package com.bustle.web.mbean.request;

import java.io.IOException;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import com.bustle.facade.proxy.AuthenticationDtoManagerProxy;
import com.bustle.user.dto.address.AddressDtoDao;
import com.bustle.user.dto.user.UserDto;
import com.bustle.web.Report;
import com.bustle.web.mbean.session.CartMBean;
import com.bustle.web.mbean.session.UserMBean;

@ManagedBean
@RequestScoped
public class MonCompteMBean
{

  private UserDto user;
  private boolean displayView = true;
  private boolean displayUpdate = false;
  @ManagedProperty("#{userMBean}")
  private UserMBean userMBean;
  @ManagedProperty("#{cartMBean}")
  private CartMBean cartMBean;

  @EJB
  private AuthenticationDtoManagerProxy authenticationDtoManager;

  @EJB
  private AddressDtoDao addressDtoDao;

  protected String firstName, lastName, email, city, name_street, postcode;
  protected Date birthday;


  @PostConstruct
  public void postBuild()
  {
    try
    {
      if (userMBean.getId() == null)
      {
        FacesContext.getCurrentInstance().getExternalContext().redirect("identification.jsf");
      }
      else
      {
        user = authenticationDtoManager.getUser(userMBean.getId());
        firstName = user.getFirstName();
        lastName = user.getLastName();
        birthday = user.getBirthday();
        email = user.getEmail();
        name_street = user.getAddress().getName();
        postcode = user.getAddress().getPostcode();
        city = user.getAddress().getCity();
      }
    }
    catch (IOException anException)
    {
      anException.printStackTrace();
    }
  }

  public String logout()
  {
    userMBean.setId(null);
    cartMBean.reset();
    return "identification.jsf";
  }

  public String save()
  {
    System.out.println("save");
    try
    {
      user.setFirstName(firstName);
      user.setLastName(lastName);
      user.setBirthday(birthday);
      user.setEmail(email);
      user.getAddress().setName(name_street);
      user.getAddress().setPostcode(postcode);
      user.getAddress().setCity(city);
      authenticationDtoManager.updateUser(user);
      addressDtoDao.update(user.getAddress());
      displayView = true;
      displayUpdate = false;
      return null;
    }
    catch (Exception anException)
    {
      Report.error("Unable to save " + "firstName" + " " + "lastName", anException);
    }

    return null;
  }

  public String modify()
  {
    displayView = false;
    displayUpdate = true;
    return null;
  }


  public AuthenticationDtoManagerProxy getAuthenticationManager()
  {
    return authenticationDtoManager;
  }

  public void setAuthenticationManager(AuthenticationDtoManagerProxy authenticationManager)
  {
    this.authenticationDtoManager = authenticationManager;
  }

  public AddressDtoDao getAddressDtoDao()
  {
    return addressDtoDao;
  }

  public void setAddressDtoDao(AddressDtoDao addressDtoDao)
  {
    this.addressDtoDao = addressDtoDao;
  }

  public String getFirstName()
  {
    return firstName;
  }


  public void setFirstName(String firstName)
  {
    this.firstName = firstName;
  }


  public String getLastName()
  {
    return lastName;
  }


  public void setLastName(String lastName)
  {
    this.lastName = lastName;
  }


  public String getEmail()
  {
    return email;
  }


  public void setEmail(String email)
  {
    this.email = email;
  }


  public String getCity()
  {
    return city;
  }


  public void setCity(String city)
  {
    this.city = city;
  }


  public String getName_street()
  {
    return name_street;
  }


  public void setName_street(String name_street)
  {
    this.name_street = name_street;
  }


  public String getPostcode()
  {
    return postcode;
  }


  public void setPostcode(String postcode)
  {
    this.postcode = postcode;
  }


  public Date getBirthday()
  {
    return birthday;
  }


  public void setBirthday(Date birthday)
  {
    this.birthday = birthday;
  }


  public UserDto getUser()
  {
    return user;
  }

  public void setUser(UserDto user)
  {
    this.user = user;
  }

  public boolean isDisplayView()
  {
    return displayView;
  }


  public void setDisplayView(boolean displayView)
  {
    this.displayView = displayView;
  }


  public boolean isDisplayUpdate()
  {
    return displayUpdate;
  }


  public void setDisplayUpdate(boolean displayUpdate)
  {
    this.displayUpdate = displayUpdate;
  }


  public UserMBean getUserMBean()
  {
    return userMBean;
  }

  public void setUserMBean(UserMBean userMBean)
  {
    this.userMBean = userMBean;
  }

  public void setCartMBean(CartMBean aCartMBean)
  {
    cartMBean = aCartMBean;
  }


}
